package com.airbat.prize.security.core.util;

import cn.hutool.core.util.StrUtil;
import com.airbat.prize.security.core.LoginUser;
import com.airbat.prize.web.web.core.util.WebFrameworkUtils;
import org.springframework.lang.Nullable;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * 安全服务工具类
 *
 * @author
 */
public class SecurityFrameworkUtils {

    private SecurityFrameworkUtils() {}

    /**
     * 从请求中，获得认证 Token
     *
     * @param request 请求
     * @param header 认证 Token 对应的 Header 名字
     * @return 认证 Token
     */
    public static String obtainAuthorization(HttpServletRequest request, String header) {
        String authorization = request.getHeader(header);
        if (!StringUtils.hasText(authorization)) {
            return null;
        }
        int index = authorization.indexOf("Bearer ");
        if (index == -1) { // 未找到
            return null;
        }
        return authorization.substring(index + 7).trim();
    }

    /**
     * 获取当前用户
     *
     * @return 当前用户
     */
    @Nullable
    public static LoginUser getLoginUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            return null;
        }
        Authentication authentication = context.getAuthentication();
        if (authentication == null) {
            return null;
        }
        return authentication.getPrincipal() instanceof LoginUser ? (LoginUser) authentication.getPrincipal() : null;
    }

    /**
     * 获得当前用户的编号，从上下文中
     *
     * @return 用户编号
     */
    @Nullable
    public static Long getLoginUserId() {
        LoginUser loginUser = getLoginUser();
        return loginUser != null ? loginUser.getId() : null;
    }

    /**
     * 获得当前用户的角色编号数组
     *
     * @return 角色编号数组
     */
    @Nullable
    public static Set<Long> getLoginUserRoleIds() {
        LoginUser loginUser = getLoginUser();
        return loginUser != null ? loginUser.getRoleIds() : null;
    }

    /**
     * 设置当前用户
     *
     * @param loginUser 登陆用户
     * @param request 请求
     */
    public static void setLoginUser(LoginUser loginUser, HttpServletRequest request) {
        // 创建 UsernamePasswordAuthenticationToken 对象
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
                loginUser, null, null);
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        // 设置到上下文
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        // 额外设置到 request 中，用于 ApiAccessLogFilter 可以获取到用户编号；
        // 原因是，Spring Security 的 Filter 在 ApiAccessLogFilter 后面，在它记录访问日志时，线上上下文已经没有用户编号等信息
        WebFrameworkUtils.setLoginUserId(request, loginUser.getId());
    }
    public static String getAreaCodeByName(String deptname){
        if (StrUtil.isNotBlank(deptname)) {
            if (deptname.indexOf("永嘉") != -1) {
                return "330324%";
            } else if (deptname.indexOf("泰顺") != -1) {
                return "330329";
            } else if (deptname.indexOf("洞头") != -1) {
                return "330305";
            } else if (deptname.indexOf("瓯海") != -1) {
                return "330304";
            } else if (deptname.indexOf("鹿城") != -1) {
                return "330302";
            } else if (deptname.indexOf("龙湾") != -1) {
                return "330303";
            } else if (deptname.indexOf("瑞安") != -1) {
                return "330381";
            } else if (deptname.indexOf("苍南") != -1) {
                return "330327";
            } else if (deptname.indexOf("龙港") != -1) {
                return "330383";
            } else if (deptname.indexOf("乐清") != -1) {
                return "330382";
            } else if (deptname.indexOf("平阳") != -1) {
                return "330326";
            } else if (deptname.indexOf("文成") != -1) {
                return "330328";
            } else if (deptname.indexOf("瓯江口") != -1) {
                return "330394";
            } else if (deptname.indexOf("机场") != -1) {
                return "330396";
            } else if (deptname.indexOf("经") != -1) {
                return "330399";
            }
        }
        return "999999";
    }
}
